<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { statisticsZaoDaoUsage } from '../../api/homePage/home'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      notUsed:0,
      usedAlready:0
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getZaoDaoCk();
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '早到仓库使用分析',
          left: 'center',
          textStyle:{
            fontSize:12
          }
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left',
          top: '12%'
        },
        series: [
          {
            center:['50%', '65%'],
            name: 'Access From',
            type: 'pie',
            radius: '50%',
            data: [
              { value: this.usedAlready, name: 'Use' },
              { value: this.notUsed, name: 'Leave Unused' }
            ],
            color: ['#CD5C5C', '#4F94CD'],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    },

    //查询早到仓库使用情况
    getZaoDaoCk(){
      statisticsZaoDaoUsage().then(response => {
        this.usedAlready = Number(response.data.usedAlready);
        this.notUsed = Number(response.data.notUsed);
        this.initChart();
      })
    },
  }
}
</script>
